#!/bin/bash
#
# install-mediawiki
#
# See also:
#   https://help.ubuntu.com/community/MediaWiki

set -e
set -x

# Skip prompt for the MySQL root password
export DEBIAN_FRONTEND=noninteractive

# Install packages.
sudo -E apt-get install -y mediawiki imagemagick mediawiki-math mysql-server

sudo perl -pi.bak -e 's%^#(DocumentRoot )/var/www%$1/var/lib/mediawiki/%' \
  /etc/apache2/sites-available/default

# OR
# perl -pi.bak -e 's%^#(Alias /mediawiki /var/lib/mediawiki.*)%$1%' \
#  /etc/apache2/conf.d/mediawiki*.conf

# Install Apache SSL server using a companion script.

$(dirname $0)/install-apache-ssl

# Visit and configure

set +x

host=$(curl http://169.254.169.254/2007-12-15/meta-data/public-hostname/)
cat <<EOM

Please visit:

  https://$host/mediawiki/

Set up the wiki by filling out the required fields.
Check "[X] Use superuser account" and fill in your MySQL root password.

EOM

while test ! -f /var/lib/mediawiki*/config/LocalSettings.php; do
  echo -n "When you see 'Installation successful!' press Enter:"
  read
done

set -x

# Wrap up

mv /var/lib/mediawiki*/config/LocalSettings.php /etc/mediawiki*/
chmod 640 /etc/mediawiki*/LocalSettings.php

set +x

echo "MediaWiki installed: http://$host/mediawiki/"
